//
//  RCBookingHeaderSectionView.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-3.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCBookingHeaderSectionView.h"

@implementation RCBookingHeaderSectionView{
    NSArray *_titles;
}

@synthesize delegate = _delegate;


- (id)initWithFrame:(CGRect)frame andTitlesArray:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _titles = [titles retain];
        NSInteger totalCount = [titles count];
        for (int i = 0; i < totalCount; i ++) {
            CGFloat orginX = frame.size.width/totalCount  * i;
            [self createSubBtn:[titles objectAtIndex:i] withFrame:CGRectMake(orginX, 0, frame.size.width/totalCount, frame.size.height) andIndex:i];
        }
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) createSubBtn:(NSString *)subTitle withFrame:(CGRect)subFrame andIndex:(NSInteger)index
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = subFrame;
    btn.tag = index + 100;
    [self addSubview:btn];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:subTitle forState:UIControlStateNormal];
    if (index == 0) {
        [btn setBackgroundColor:[UIColor yellowColor]];
    }else{
        [btn setBackgroundColor:[UIColor redColor]];
    }
    
}

- (void) btnClick:(id)sender
{
    UIButton *btn = sender;
    NSInteger index = btn.tag - 100;
    if ([_delegate respondsToSelector:@selector(userDidClickAtIndex:)]) {
        [_delegate userDidClickAtIndex:index];
    }
}
@end
